package com.adarsh.jpa.listener;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;


public class GuestListener implements ServletContextListener{
	private static EntityManagerFactory emf;
	@Override
	public void contextDestroyed(ServletContextEvent ctxEvent) {
		//EntityManagerFactory emf = (EntityManagerFactory)ctxEvent.getServletContext().getAttribute("emf");
		emf.close();
	}

	@Override
	public void contextInitialized(ServletContextEvent ctxEvent) {
		emf = Persistence.createEntityManagerFactory("samplePU");
	}

	public static EntityManager getEntityManager() {
		return emf.createEntityManager();
	}

}
